<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.googlecode.objectify.Objectify" %>
<%@ page import="com.googlecode.objectify.ObjectifyService" %>

<%@ page import="edu.caltech.cs.techerlink.Post" %>
<%@ page import="edu.caltech.cs.techerlink.Course" %>

<%@ page import="edu.caltech.cs.techerlink.PMF" %>

<html>
<style type="text/css">
<!--
.font1 {
	font-family: Ayuthaya;
}
body p {
	font-family: Ayuthaya;
}
.font2 {
	font-family: Ayuthaya;
}
-->
</style>
<body>


<hr align="left" width="600" size="10" noshade>
<h1 align="center" class="font2">Techerlink</h1>
<hr align="right" width="600" size="10" noshade>
<p>&nbsp;</p>
<p>
  <%
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if (user == null) {
    %>
</p>
    <p class="font1">Hello! You must
	<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">sign in</a>
to use the Techerlink collaboration site.</p>
<%
} else {

	%>
	<p align="right">Hello, <%= user.getNickname() %>! (You can
	<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>)</p>

	<table width="997" border="0">
	  <tr>
	    <td width="272" height="115" valign="top" class="font1"><p>My Courses</p>
          <hr align="left" width="200" size="6" noshade>
          <p>&nbsp;</p>
          <hr align="right" width="200" size="6" noshade>
          <p>&nbsp;</p>
        <p>&nbsp;</p></td>
	    <td width="715" class="font1">
	    <iframe src="https://www.google.com/calendar/embed?height=600&amp;wkst=1&amp;bgcolor=%23FFFFFF&amp;src=kpf5i04fo73uh70jhvn2mupu70%40group.calendar.google.com&amp;color=%23125A12&amp;src=122hlocn1ura8tges29ghthsdc%40group.calendar.google.com&amp;color=%238C500B&amp;ctz=America%2FLos_Angeles" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"></iframe>
      </tr>
	  <tr>
	    <td height="143" valign="top" class="font1"><p>Add new course</p>
	      <hr align="left" width="200" size="6" noshade>
	      <p>Department: 
	        <select name="1" size="1" id="1">
	          <option selected>----</option>
	          <option>Math</option>
	          <option>CS</option>
            </select>
	      </p>
        <hr align="right" width="200" size="6" noshade>
        <table width="268" border="0">
          <tr>
            <td width="194">Course: </td>
            <td width="64"><select name="2" id="2">
              <option>----</option>
            </select></td>
          </tr>
        </table></td>
	    <td class="font1">Wall will go here (see below)</td>
      </tr>
</table>
	<p>&nbsp; </p>
	<p>&nbsp;</p>
	<p>Please enter your preferred nickname: </p>
	<form action="/student" method="post">
    	<div><textarea name="newName" rows="1" cols="30"></textarea></div>
    	<div><input type="submit" value="Change name" /></div>
    </form>

	<form action="/post" method="post">
    	<div><textarea name="content" rows="3" cols="60"></textarea></div>
    	<div><input type="submit" value="Post" /></div>
	</form>
  
	<form>
    	<input type=button value="Refresh" onClick="window.location.reload()">
	</form>

	<%
	PersistenceManager pm = PMF.get().getPersistenceManager();
/*  Uncomment this to revert student to using the pmf  
	String query1 = "select from " + Student.class.getName();
    List<Student> students = (List<Student>) pm.newQuery(query1).execute();

    if (students.isEmpty()) {
		%> <p>No students yet. You are still really dumb!</p> <%
    }
	for (Student s : students) {
		%> <p><b><%= s.getNickname() %></b> </p> <%
   	}
*/   	
    String query2 = "select from " + Post.class.getName() + " order by date desc range 0,10";
    List<Post> posts = (List<Post>) pm.newQuery(query2).execute();

    if (posts.isEmpty()) {
		%>
		<p>No messages yet. Be the first one to post!</p>
		<%
    } else {
        for (Post g : posts) {
            if (g.getAuthor() == null) {
				%> <p>An anonymous person wrote:</p> <%
            } else {
				%> <p><b><%= g.getAuthor().getNickname() %></b> wrote:</p> <%
            }
		%> <blockquote><%= g.getContent() %></blockquote> <%
        }
    }
    pm.close();
}
%>

</body>
</html>